
package com.fivebrms.view.independent
{
	import org.osflash.thunderbolt.Logger;
	
	import mx.controls.Button;
	import mx.controls.CheckBox;
	import mx.controls.TextInput;
	import mx.controls.Text;
	import mx.controls.TextArea;
	import mx.containers.VBox;
	
	import mx.controls.Alert;
	
	import mx.utils.StringUtil;
	
	import com.fivebrms.view.independent.FormFields;
	import com.fivebrms.view.independent.About;
	import com.fivebrms.view.independent.ContractText;
	import com.fivebrms.view.independent.validators.IndependentValidator;
	
	import mx.validators.ValidationResult;
	
	import flash.net.registerClassAlias;  
	import mx.utils.ObjectUtil;
	import mx.binding.utils.BindingUtils;

	import com.fivebrms.events.IndependentEvent;
	import mx.events.FlexEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import mx.events.ValidationResultEvent;
	
	import com.fivebrms.model.MainModelLocator;
	
	public class IndependentVBox extends VBox
	{
		private var model : MainModelLocator = MainModelLocator.getInstance();
		
		//
		// from the About.mxml
		public var aboutText_field_orig : String;
		public var aboutText_field : String;
		
		public var contractHtmlText : ContractText;
		private var indepValidator : IndependentValidator;
		
		public function IndependentVBox()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
		}
		
		//
		// attach all the event listeners here
		//
        private function creationCompleteHandler(event:FlexEvent) : void
        {
            Logger.info("IndependentVBox..1");

			//
			// create the form validator..and pass in the VBox containing the input
			// fields to be validated
			//
			this.indepValidator = new IndependentValidator( (this.getChildByName("formFields") as FormFields) );
			
			//
			// generate the text to put into the ui text display control
			//
			this.contractHtmlText = new ContractText();
			
			var contractorName_field : TextInput = (this.getChildByName("formFields") as FormFields).inp_contractorName;
			var contractorAddress_field : TextInput = (this.getChildByName("formFields") as FormFields).inp_contractorAddress;
			var contractorButton_field : Button = (this.getChildByName("formFields") as FormFields).btn_submitIndependentForm;
			
			//
			// copy the string holding the agreement text into a temporary variable
			//
			var temp_aboutText_field : String = this.contractHtmlText.contractText_htmlText;
			
			Logger.info("..222..in the bindFormFields independent.." + temp_aboutText_field );
			
			//
			// deep copy of agreement text..hold original text into a placeholder variable
			//
			registerClassAlias("String", String);
			this.aboutText_field_orig = String(ObjectUtil.copy(temp_aboutText_field) );

			Logger.info("..333..in the bindFormFields independent..>" + this.aboutText_field_orig );

			//
			// as the form fields are filled in..update the agreement text with the
			// input fields
			//
			BindingUtils.bindSetter(bindFormFields, contractorName_field, "text");
			BindingUtils.bindSetter(bindFormFields, contractorAddress_field, "text");
			
			//
			// validate the fields as soon as one of the fields is typed into
			//
			contractorName_field.addEventListener(Event.CHANGE, this.validateIndependentFormFields);
			contractorAddress_field.addEventListener(Event.CHANGE, this.validateIndependentFormFields);
			
			//
			// validate the form when this button is pressed
			//
			contractorButton_field.addEventListener(MouseEvent.CLICK, this.buttonPressedHandler);
			
            Logger.info("IndependentVBox..5");
		}
		
		private function buttonPressedHandler(event : MouseEvent ) : void
		{
            Logger.info("you clicked the contractorButton_field button!!!");
            
			var validationResult : ValidationResultEvent = this.indepValidator.validate( event.currentTarget );
	        //
	        // forms have been successfully filled in
	        //
	        if ( validationResult.results == null )
            {
            	Alert.show("The form has been successfully submitted.");
            
			    //
			    // generate the pdf
			    //
	            var fileName : String = "independent.pdf";
				(this.getChildByName("formFields") as FormFields).exportPdf( fileName );
				            
	            //
	            // submit the confidentiality form to the server
	            //            
	            var indepEvent : IndependentEvent = new IndependentEvent( );
	            indepEvent.dispatch();
	            
	            
	            //
	            // disable the whole entire control so that form cannot be
	            // double submitted
	            //
	            (this.getChildByName("formFields") as FormFields).enabled = false;	            
            }
            else
            {
            	Alert.show("This form has yet to be filled out.");
            }
            
			Logger.info("buttonPressedHandler independent contractor FormFields  button!!!");
            
		}
		private function bindFormFields( field : String ) : void
		{
			Logger.info("..1..in the bindFormFields independent..");
			
			//
			// deep copy of agreement text
			//
			registerClassAlias("String", String);
			this.aboutText_field = String(ObjectUtil.copy(this.aboutText_field_orig) );
			
			Logger.info("bindFormFields..111..>" + this.aboutText_field );
			
			var contractorName_field : TextInput = (this.getChildByName("formFields") as FormFields).inp_contractorName;
			var contractorAddress_field : TextInput = (this.getChildByName("formFields") as FormFields).inp_contractorAddress;

			var result : String = StringUtil.substitute(this.aboutText_field, contractorName_field.text, contractorAddress_field.text );
			
			Logger.info("bindFormFields..222..>" + result );
			
			(this.getChildByName("aboutFields") as About).htmlFormFields.htmlText = result;
			
			Logger.info("..1..in the bindFormFields independent..");
		}

		private function validateIndependentFormFields( event : Event ) : void
		{
			//
			// the event.currentTarget is not actually used but the "validate" method which is
			// overrided requires a parameter
			//
			var validationResult : ValidationResultEvent = this.indepValidator.validate( event.currentTarget );
			//
			// not using this currently
			//
			var results : Array = validationResult.results;

		}
		
	}
}

